<template>
  <page-header-wrapper>
    <page-tab-list>
      <div class="table-page-search-box">
        <a-form layout="inline">
          <a-row :gutter="24">
            <a-col :md="8" :sm="24">
              <a-form-item label="发放时间">
                <a-range-picker
                  style="width:100%;"
                  format="YYYY-MM-DD HH:mm"
                  valueFormat="YYYY-MM-DD HH:mm"
                  :placeholder="['开始时间', '结束时间']"
                  v-model="queryTime"
                  @change="changeTime"
                />
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="期数时间">
                <a-date-picker
                  style="width:100%;"
                  v-model="queryParam.pool_time"
                  format="YYYY-MM-DD HH:mm"
                  valueFormat="YYYY-MM-DD HH:mm"
                  placeholder="请选择期数时间"
                />
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="等级">
                <div class="select">
                  <a-select v-model="queryParam.level_id">
                    <a-select-option :value="item.id" v-for="item in levelList" :key="item.id">
                      {{ item.name }}
                    </a-select-option>
                  </a-select>
                  <a-button type="primary" @click="getList">查询</a-button>
                </div>
              </a-form-item>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </page-tab-list>
    <a-card :bordered="false">
      <a-table
        :rowKey="
          (r, i) => {
            return i
          }
        "
        :pagination="{
          total: this.totalCount,
          showSizeChanger: false,
          onChange: this.tablePageChange,
        }"
        :columns="columns"
        :data-source="list"
        :scroll="{ x: 'max-content' }"
      >
        <div slot="name" slot-scope="text, record">
          <div class="name">
            <div class="pic">
              <img :src="record.avatar" alt="" />
            </div>
            <div class="info">
              <span>{{ record.nickname }}</span>
            </div>
          </div>
        </div>
        <span slot="send_time" slot-scope="text">
          {{ text > 0 ? $moment(text * 1000).format('YYYY-MM-DD HH:mm:ss') : '-' }}
        </span>
        <span
          slot="money"
          slot-scope="text, scope"
          class="level_one_count"
          style="color: #262626"
          @click="$router.push('/plugins/shareholder/bonus?id=' + scope.id)"
        >
          ￥{{ text || 0 }}
        </span>
        <span slot="user_list" slot-scope="text, scope" class="level_one_count" @click="showCount(scope)">
          {{ scope.user_list.length }} （点击查看）
        </span>
        <span slot="is_send" slot-scope="text">
          {{ text == 0 ? '待发放' : '已发放' }}
        </span>
        <div slot="action" slot-scope="text, scope">
          <div class="action">
            <a-button type="link" @click="send(scope)" size="small" :disabled="scope.is_send == 1">发放</a-button>
          </div>
        </div>
      </a-table>
    </a-card>
    <!-- 显示下级人数弹窗 -->
    <a-modal v-model="showJunior" title="下级人数" :destroyOnClose="true">
      <a-row v-for="(item, index) in juniorList" :key="index" style="margin-bottom: 10px">
        <div class="name">
          <div class="pic">
            <img :src="item.avatar" alt="" />
          </div>
          <div class="info">
            <span>{{ item.name }}</span>
          </div>
        </div>
      </a-row>
    </a-modal>
  </page-header-wrapper>
</template>

<script>
import { allLevel, pool, sendPool } from '@/api/shareholder'
const columns = [
  {
    title: 'ID',
    dataIndex: 'id',
  },
  {
    title: '期数',
    dataIndex: 'time',
  },
  {
    title: '等级',
    dataIndex: 'level_name',
  },
  {
    title: '股东分红金额',
    dataIndex: 'money',
    scopedSlots: {
      customRender: 'money',
    },
  },
  {
    title: '参与分红人数',
    scopedSlots: {
      customRender: 'user_list',
    },
  },
  {
    title: '发放状态',
    dataIndex: 'is_send',
    scopedSlots: {
      customRender: 'is_send',
    },
  },
  {
    title: '发放时间',
    dataIndex: 'send_time',
    scopedSlots: {
      customRender: 'send_time',
    },
    sorter: (a, b) => a.audit_time - b.audit_time,
  },
  {
    title: '操作',
    dataIndex: 'action',
    scopedSlots: {
      customRender: 'action',
    },
  },
]
export default {
  data() {
    return {
      // 搜索参数
      queryParam: {
        send_start_time: null,
        send_end_time: null,
        pool_time: null,
        level_id: null,
        pageNo: 1,
        pageSize: 10,
      },
      // 请求的数据列表
      list: [],
      // 表格列配置
      columns,
      // 数据条数
      totalCount: 0,
      // 所有等级列表
      levelList: [],
      // 选择的时间
      queryTime: [],
      // 展示分红人数弹窗
      showJunior: false,
      // 分红人数列表
      juniorList: [],
    }
  },
  methods: {
    // 获取数据列表
    async getList() {
      const hide = this.$message.loading('加载中', 0)
      const res = await pool(this.queryParam)
      if (res.code == 0) {
        this.totalCount = res.data.totalCount
        this.list = res.data.data
      }
      hide()
    },
    // 分页请求
    tablePageChange(e) {
      this.queryParam.pageNo = e
      this.getList()
    },
    // 获取所有等级列表
    async allLevel() {
      const res = await allLevel()
      if (res.code == 0) {
        this.levelList = res.data
      }
    },
    // 切换筛选时间
    changeTime(e) {
      if (e.length) {
        this.queryParam.send_start_time = e[0]
        this.queryParam.send_end_time = e[1]
      } else {
        this.queryParam.send_start_time = null
        this.queryParam.send_end_time = null
      }
    },
    // 显示下级列表弹窗
    showCount(e) {
      this.$router.push('/plugins/shareholder/join?id=' + e.id)
      // if(e.is_send == 1) {

      // }
    },
    // 点击发放
    send(e) {
      let that = this
      this.$confirm({
        title: '是否发放分红?',
        async onOk() {
          const res = await sendPool({ id: e.id })
          if (res.code == 0) {
            that.getList()
          }
        },
        onCancel() {},
      })
    },
  },
  mounted() {
    this.allLevel()
    this.getList()
  },
}
</script>

<style lang="less" scoped>
.search {
  /deep/ .ant-card-body {
    padding-top: 0 !important;
    padding-bottom: 14px !important;
  }
}
.searchBox {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  /deep/ .ant-input-affix-wrapper {
    width: 220px;
    margin-top: 14px;
    margin-right: 20px;
  }
  .select {
    display: flex;
    align-items: center;
    margin-top: 14px;
    margin-right: 20px;
  }
  .ant-btn {
    margin-top: 14px;
  }
}
.name {
  display: flex;
  align-items: center;
  .pic {
    width: 48px;
    min-width: 48px;
    height: 48px;
    border-radius: 4px;
    overflow: hidden;
    margin-right: 24px;
    img {
      width: 100%;
      height: 100%;
    }
  }
  .info {
    flex: auto;
    width: 0;
    display: flex;
    flex-direction: column;
    font-size: 14px;
    span {
      width: 100%;
      overflow: hidden;
      white-space: nowrap;
      text-overflow: ellipsis;
    }
    span:nth-child(2) {
      color: rgba(0, 0, 0, 0.45);
      margin-top: 6px;
    }
  }
}
.action {
  display: flex;
  align-items: center;
  .line {
    width: 1px;
    height: 14px;
    background-color: #d8d8d8;
  }
}
.level_one_count {
  text-decoration: underline;
  cursor: pointer;
  color: #ff4400;
}
</style>